package edu.sdjzu.exam.service.message;

import edu.sdjzu.exam.domain.group.UserGroupMember;
import edu.sdjzu.exam.dto.message.SystemMessage;
import edu.sdjzu.exam.mapper.group.UserGroupMemberMapper;
import edu.sdjzu.exam.mapper.message.SystemMessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemMessageService {

    private final SystemMessageMapper messageMapper;
    @Autowired
    UserGroupMemberMapper userGroupMemberMapper;
    public SystemMessageService(SystemMessageMapper messageMapper) {
        this.messageMapper = messageMapper;
    }

    // 新增消息
    public void addMessage(SystemMessage message) {
        messageMapper.insert(message);
    }

    // 标记为已读
    public void markAsRead(Long id) {
        messageMapper.updateStatus(id, 1);
    }

    // 标记为未读
    public void markAsUnread(Long id) {
        messageMapper.updateStatus(id, 0);
    }

    // 查询已读消息
    public List<SystemMessage> getReadMessages(String studentId) {
        return messageMapper.findReadMessages(studentId);
    }

    // 查询未读消息
    public List<SystemMessage> getUnreadMessages(String studentId) {
        return messageMapper.findUnreadMessages(studentId);
    }

    public void addMessageByaGroup(String message,Long groupId) {
        List<UserGroupMember> userGroupMembers = userGroupMemberMapper.selectMembersByGroupId(groupId);
        for (UserGroupMember userGroupMember : userGroupMembers) {
            SystemMessage systemMessage = new SystemMessage();
            systemMessage.setStudentId(userGroupMember.getUserId());
            systemMessage.setContent(message);
            messageMapper.insert(systemMessage);
        }
    }
}
